package hot;

/**
 * @author lakersUp
 * @Classname      两数相加(链表逆序存储)
 * @Description     直接加,维护一个变量
 * @Date 2022/3/7 9:51
 * @Version 1.0
 */
  class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
public class Hot2addTwoNumbers {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(-1),pre=dummyHead;
        int t=0;
        while (l1!=null || l2!=null || t!=0){
            if(l1!=null){
                t+=l1.val;
                l1=l1.next;
            }
            if(l2!=null){
                t+=l2.val;
                l2=l2.next;
            }
            pre.next=new ListNode(t%10);
            pre=pre.next;
            t/=10;
        }
        return dummyHead.next;
    }
}
